package com.example.com.daervideo.fragments;

import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.RelativeLayout;

import com.example.com.daervideo.BR;
import com.example.com.daervideo.R;
import com.example.com.daervideo.activitys.MainActivity;
import com.example.com.daervideo.databinding.FragmentMainBinding;
import com.example.com.daervideo.fragments.video.VideoFragment;
import com.example.com.daervideo.popuwindow.PublishPpWindow;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;

/**
 * Created by i7-1 on 2018/12/7.
 */

public class MainFragment extends BaseFragment<FragmentMainBinding, BaseViewModel> {

    private FragmentManager manager;
    private UserFragment userFragment;
    private MainActivity mainActivity;
    private VideoFragment videoFragment;
    private RelativeLayout bottom_sheet;
    private BottomSheetBehavior<RelativeLayout> behavior;
    private ArrayList<Fragment> fragments;
    private PublishPpWindow publishPpWindow;
    private ArrayList<Fragment> list;
    Disposable subscribe = RxBus.getDefault().toObservable(Integer.class)
            .subscribe(new Consumer<Integer>() {
                @Override
                public void accept(Integer integer) throws Exception {
                    getBinding().mfVp.setCurrentItem(integer);
                }
            });


    @Override
    public int initContentView() {
        return R.layout.fragment_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public BaseViewModel initViewModel() {
        return new BaseViewModel(context);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.e("mainfragment", "setUserVisibleHint=" + isVisibleToUser);
        if (videoFragment != null) {
            videoFragment.setUserVisibleHint(isVisibleToUser);
        }
    }

    @Override
    public void initData() {
        super.initData();

        //将订阅者加入管理站
        RxSubscriptions.add(subscribe);

        list = new ArrayList<>();
        mainActivity = (MainActivity) context;
        videoFragment = new VideoFragment();
        userFragment = new UserFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("userType", 1);
        userFragment.setArguments(bundle);

        list.add(new CollectionFragmrnt());
        list.add(videoFragment);
        list.add(userFragment);
        getBinding().mfVp.setAdapter(new HomeFAdapter(getChildFragmentManager()));
        getBinding().mfVp.setCurrentItem(1);
        getBinding().mfVp.setOffscreenPageLimit(2);


        getBinding().mfVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    mainActivity.showTab();
                } else {
                    mainActivity.hideTab();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }


    private class HomeFAdapter extends FragmentPagerAdapter {
        public HomeFAdapter(FragmentManager childFragmentManager) {
            super(childFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }
}
